//
//  ClassifyDetailCell.m
//  Everest
//
//  Created by 李其昌 on 14/12/17.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "ClassifyDetailCell.h"
#import "ClassifyDetail.h"
#import "UIImageView+WebCache.h"

@interface ClassifyDetailCell ()

@property (nonatomic, retain) UILabel *myBackLabel;
@property (nonatomic, retain) UIImageView *myMaxImageView;
@property (nonatomic, retain) UIImageView *myMinImageView;
@property (nonatomic, retain) UILabel *myTitleLabel;
@property (nonatomic, retain) UILabel *myNumberLabel;
@property (nonatomic, retain) UILabel *myUpdataLabel;
@property (nonatomic, retain) UILabel *myTimeLabel;
@property (nonatomic, retain) UIButton *myButton;

@end

@implementation ClassifyDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.contentView.backgroundColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0];
        
        self.myBackLabel = [[UILabel alloc] init];
        self.myBackLabel.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.myBackLabel];
        [_myBackLabel release];
        
        self.myMaxImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.myMaxImageView];
        [_myMaxImageView release];
        
        self.myMinImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.myMinImageView];
        [_myMinImageView release];
        
        self.myTitleLabel = [[UILabel alloc] init];
        self.myTitleLabel.font = [UIFont systemFontOfSize:16];
//        self.myTitleLabel.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:self.myTitleLabel];
        [_myTitleLabel release];
        
        self.myNumberLabel = [[UILabel alloc] init];
        self.myNumberLabel.font = [UIFont systemFontOfSize:13];
        self.myNumberLabel.textColor = [UIColor colorWithRed:169/255.0 green:169/255.0 blue:169/255.0 alpha:1.0];
//        self.myNumberLabel.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:self.myNumberLabel];
        [_myNumberLabel release];
        
        self.myUpdataLabel = [[UILabel alloc] init];
        self.myUpdataLabel.font = [UIFont systemFontOfSize:13];
        self.myUpdataLabel.textColor = [UIColor colorWithRed:169/255.0 green:169/255.0 blue:169/255.0 alpha:1.0];
        [self.contentView addSubview:self.myUpdataLabel];
        [_myUpdataLabel release];
        
        self.myTimeLabel = [[UILabel alloc] init];
        self.myTimeLabel.font = [UIFont systemFontOfSize:13];
        self.myTimeLabel.textColor = [UIColor colorWithRed:169/255.0 green:169/255.0 blue:169/255.0 alpha:1.0];
//        self.myTimeLabel.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:self.myTimeLabel];
        [_myTimeLabel release];
        
        self.myButton = [UIButton buttonWithType:UIButtonTypeSystem];
//        self.myButton.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:self.myButton];
        
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.myBackLabel.frame = CGRectMake(0, 5, self.contentView.bounds.size.width, self.contentView.bounds.size.height - 10);
    
    self.myMaxImageView.frame = CGRectMake(0, 5, self.contentView.bounds.size.width / 3 - 10, self.contentView.bounds.size.height - 10);
    
    self.myMinImageView.frame = CGRectMake((self.contentView.bounds.size.width / 3) + 10, 15, 21, 21);
    
    self.myTitleLabel.frame = CGRectMake(self.contentView.bounds.size.width / 3 + 31, 0, self.contentView.bounds.size.width - (self.contentView.bounds.size.width / 3 + 30), self.contentView.bounds.size.height / 2);
    self.myTitleLabel.numberOfLines = 0;
    
    self.myNumberLabel.frame = CGRectMake(self.contentView.bounds.size.width / 3 + 10, self.contentView.bounds.size.height / 2, self.contentView.bounds.size.width / 3, self.contentView.bounds.size.height / 4);
    
    self.myUpdataLabel.frame = CGRectMake(self.contentView.bounds.size.width / 3 + 10, self.contentView.bounds.size.height * 3 / 4, self.contentView.bounds.size.width / 5.4, self.contentView.bounds.size.height / 4);
    
    self.myTimeLabel.frame = CGRectMake(self.contentView.bounds.size.width / 3 + 10 + self.contentView.bounds.size.width / 5.4, self.contentView.bounds.size.height * 3 / 4, self.contentView.bounds.size.width / 4, self.contentView.bounds.size.height / 4);
    
//    self.myButton.frame = CGRectMake(self.contentView.bounds.size.width * 3 / 4, self.contentView.bounds.size.height / 2, self.contentView.bounds.size.width / 4, self.contentView.bounds.size.height / 2);
//    [self.myButton setTitle:@"订 阅" forState:UIControlStateNormal];
//    [self.myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
}
// 点击订阅按钮的方法
- (void)buttonClicked:(UIButton *)button
{
//    NSLog(@"订阅");
}

- (void)setClassify:(ClassifyDetail *)classify
{
    if (_classify != classify) {
        [_classify release];
        
        _classify = [classify retain];
    }
    self.myTitleLabel.text = self.classify.title;
    
    NSString *classStr = [self.classify.playsCounts description];
    
    
    if (classStr.length >= 5) {
        
        float interg = [self.classify.playsCounts floatValue] / 10000;
        self.myNumberLabel.text = [NSString stringWithFormat:@"%.1f万播放次数", interg];
        
    } else {
        NSString *str = [classStr stringByAppendingString:@"播放次数"];
        self.myNumberLabel.text = str;
    }
    NSDate *date = [NSDate date];
    NSTimeInterval time = [date timeIntervalSince1970];
    float lastUptrackAt = [self.classify.lastUptrackAt floatValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *timeDate = [NSDate dateWithTimeIntervalSinceNow:- (time - lastUptrackAt / 1000)];
    NSString *stime = @"最后更新:";
    NSString *timeStr =[formatter stringFromDate:timeDate];
    self.myTimeLabel.text = timeStr;
    self.myUpdataLabel.text = stime;
    NSString *maxStr = self.classify.albumCoverUrl290;
    
    NSURL *url = [NSURL URLWithString:maxStr];
    
    [self.myMaxImageView setImageWithURL:url];
    
    NSString *minStr = @"iconfont-zhuanji.png";
    
    self.myMinImageView.image = [UIImage imageNamed:minStr];
    
}

- (void)dealloc
{
    [_myMaxImageView release];
    [_myMinImageView release];
    [_myNumberLabel release];
    [_myTitleLabel release];
    [_myTimeLabel release];
    [_myButton release];
    [super dealloc];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
